package com.ynet.middleground.contract.service.impl;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.contract.bean.GetFileReq;
import com.ynet.middleground.contract.bean.GetOssUrlReq;
import com.ynet.middleground.contract.bean.UploadReq;
import com.ynet.middleground.contract.dto.FileInfoDTO;
import com.ynet.middleground.contract.dto.OssFileDto;
import com.ynet.middleground.contract.model.OssModel;
import com.ynet.middleground.contract.service.OssService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 *
 * OSS 文件服务实现
 * 
 * @author liulx
 * @create 2021-01-29 14:47
 */
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Api(value = "OSS 文件服务", tags = {"OSS 文件服务"})
@Path("oss-file")
@Service(timeout = 16000, version = "0.0.1")
public class OssServiceImpl implements OssService, GenericService {

    @Autowired
    private OssModel ossModel;

    /**
     * 根据文件短路径获取完整路径
     *
     * @param getOssUrlReq 获取文件完整路径请求信息
     * @return 文件完整路径
     */
    @POST
    @ApiOperation(value = "获取文件完整路径", notes = "根据文件短路径获取完整路径")
    @ApiImplicitParam(name = "requestObj", dataType = "GetOssUrlReq", paramType = "body", required = true)
    @Override
    public ServiceResult<OssFileDto> getOssFullUrl(GetOssUrlReq getOssUrlReq) {
        ServiceResult<OssFileDto> result = new ServiceResult<>();
        try {
            result.setResult(ossModel.getFullUrl(getOssUrlReq));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "根据文件短路径获取完整路径服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 上传文件至 OSS 服务
     *
     * @param req 上传文件请求信息
     * @return 文件的 OSS 地址
     */
    @POST
    @Path("upload")
    @ApiOperation(value = "上传文件至 OSS 服务", notes = "上传文件至 OSS 服务")
    @ApiImplicitParam(name = "requestObj", dataType = "UploadReq", paramType = "body", required = true)
    @Override
    public ServiceResult<String> upload(@Valid UploadReq req) {
        ServiceResult<String> result = new ServiceResult<>();
        try {
            result.setResult(ossModel.upload(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "上传文件至 OSS 服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 获取文件信息流
     *
     * @param req 获取文件请求信息
     * @return 文件流传输对象
     */
    @POST
    @Path("get")
    @ApiOperation(value = "获取文件信息流", notes = "根据文件地址，获取文件信息")
    @ApiImplicitParam(name = "requestObj", dataType = "GetFileReq", paramType = "body", required = true)
    @Override
    public ServiceResult<FileInfoDTO> getFileInfo(@Valid GetFileReq req) {
        ServiceResult<FileInfoDTO> result = new ServiceResult<>();
        try {
            result.setResult(ossModel.getFileByUrl(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "获取文件信息服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECCT0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

}
